const db = require("../../lib/conn_oracle");

//查询目录信息
exports.getDirsInfo = (req,res)=>{
    let dirsSql = `select * from vue_xfjcdirs
                   order by dirlevel
                  `
    let datas
    async function fun(){
        let dirs = await db.search2(dirsSql)
        datas = {dirs}
        res.json({
            status:1,
            msg:"查询成功",
            datas,
        })
    }
    fun()
}

//处理目录信息
exports.editDirInfo = async (req,res)=>{
    // console.log(req.body);
    let {submitType,submitForm} = req.body
    let {ID,PARENT,PATH,COMPONENTPATH,COMPONENTNAME,DIRLEVEL,TITLE,REMARK,CUST_TYPE,ICON} = submitForm
    PARENT = !PARENT ? null : PARENT   
    let resDatas
    let handleSql 
    let handleData
    let msg
    let submitTypeStr

    /**
     * 数据库处理函数
     * @param {String} sql  sql语句
     * @param {Array} data  参数（以数组形式传递，批量处理使用嵌套数组）
     */
    const cudFun = async (sql,data)=>{
        let newDirs = await db.writeTab(sql,data)
        // console.log(newDirs);
        resDatas = {newDirs}        
        msg = resDatas.newDirs.errorNum ? `${submitTypeStr}失败，错误代码：ORA-` + resDatas.newDirs : `${submitTypeStr}成功`
        return{
            status:1,
            msg,
            resDatas,
        }  
    }
    if(submitType === "addOne"){
        submitTypeStr = "新增"
        handleData = [PATH,DIRLEVEL,TITLE,REMARK,CUST_TYPE,ICON]
        handleSql = `insert into c##wxw.vue_xfjcdirs(id,path,dirlevel,title,remark,cust_type,icon)
                     values('',:PATH,:DIRLEVEL,:TITLE,:REMARK,:CUST_TYPE,:ICON)
                    `    
        let newDirs = await cudFun(handleSql,handleData)
        res.json(newDirs)    
    }else if(submitType === "addSub"){
        submitTypeStr = "新增"
        handleData = [PARENT,PATH,COMPONENTPATH,COMPONENTNAME,DIRLEVEL,TITLE,REMARK,CUST_TYPE,ICON]
        handleSql = `insert into c##wxw.vue_xfjcdirs(id,parent,path,componentpath,componentname,dirlevel,title,remark,cust_type,icon)
                     values('',:PARENT,:PATH,:COMPONENTPATH,:COMPONENTNAME,:DIRLEVEL,:TITLE,:REMARK,:CUST_TYPE,:ICON)
                    `
        let newDirs = await cudFun(handleSql,handleData)
        res.json(newDirs)   
    }else if(submitType === "update"){
        submitTypeStr = "更新"
        handleData = [PARENT,PATH,COMPONENTPATH,COMPONENTNAME,DIRLEVEL,TITLE,REMARK,CUST_TYPE,ICON,ID]
        handleSql = `
                     update c##wxw.vue_xfjcdirs
                     set 
                     parent = :PARENT,
                     path = :PATH,
                     componentpath = :COMPONENTPATH,
                     componentname = :COMPONENTNAME,
                     dirlevel = :DIRLEVEL,
                     title = :TITLE,
                     remark = :REMARK,
                     cust_type = :CUST_TYPE,
                     icon = :ICON
                     where id = :ID
                    ` 
        let newDirs = await cudFun(handleSql,handleData)
        res.json(newDirs)       
    }else if(submitType === "delete"){
        submitTypeStr = "删除"
        handleData = [ID]
        handleSql = `delete c##wxw.vue_xfjcdirs where id = :ID`
        let newDirs = await cudFun(handleSql,handleData)
        if(newDirs.msg.includes("失败")){
            newDirs.msg = "目录删除失败"           
            res.json(newDirs)
        }else{
            handleSql = `delete c##wxw.vue_xfjcauthority_dirs_middle where dirid = :ID`
            let newDirs = await cudFun(handleSql,handleData)
            if(newDirs.msg.includes("失败")){
                newDirs.msg = "权限组内对应目录删除失败"           
                res.json(newDirs)
            }else{                
                newDirs.msg = "角色删除成功"           
                res.json(newDirs)
            }
        }
        // res.json(newDirs)
    } 
}